package com.tjhzzx.ibmsSupervisor.user;

import com.tjhzzx.ibmsSupervisor.user.entities.UserInfoItem;
import com.tridium.history.audit.BAuditHistoryService;
import com.tridium.hx.BHandheldHxProfile;
import org.apache.commons.lang3.StringUtils;

import javax.baja.security.AuditEvent;
import javax.baja.security.BPassword;
import javax.baja.security.BPasswordAuthenticator;
import javax.baja.sys.Sys;
import javax.baja.user.BUser;
import javax.baja.user.BUserService;
import javax.baja.web.BWebProfileConfig;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserManager {
    private static String PREFIX = "TP_";
    private static Map<String, String> roleMap = new HashMap<>();

    public static List<UserInfoItem> listUsers(BUser currentUser) {
        if (!currentUser.getPermissions().isSuperUser()) {
            return null;
        }

        BUserService userService = (BUserService) Sys.getService(BUserService.TYPE);
        BUser[] users = userService.getChildren(BUser.class);
        List<UserInfoItem> userInfoItems = new ArrayList<>();
        for (int i = 0; i < users.length; i++) {
            BUser user = users[i];
//            if ((!user.getName().startsWith(PREFIX)) && (!user.getName().equals("admin")) && (!user.getName().equals("guest"))) {
            UserInfoItem item = new UserInfoItem();
            item.name = user.getName();
            item.role = user.getRoles();
            item.phone = user.getCellPhoneNumber();
            item.email = user.getEmail();
            userInfoItems.add(item);
//            }
        }
        return userInfoItems;
    }

    public static boolean addUser(BUser currentUser, UserInfoItem userInfo) {
        if (!currentUser.getPermissions().isSuperUser()) {
            return false;
        }
        BUserService userService = (BUserService) Sys.getService(BUserService.TYPE);
        try {
            BUser myuser = new BUser();
            myuser.copyFrom(userService.getUser(userInfo.role));
            BPasswordAuthenticator pa = (BPasswordAuthenticator) myuser.getAuthenticator();
            pa.setPassword(BPassword.make(userInfo.password));
            myuser.setFullName(userInfo.role);
            myuser.setEmail(userInfo.email);
            myuser.setCellPhoneNumber(userInfo.phone);
            userService.add(userInfo.name, myuser);
            BWebProfileConfig webProfileConfig = (BWebProfileConfig) myuser.getMixIn(BWebProfileConfig.TYPE);
            webProfileConfig.setTypeSpec(BHandheldHxProfile.TYPE.getTypeSpec());
            BAuditHistoryService a = (BAuditHistoryService) Sys.getService(BAuditHistoryService.TYPE);
            a.audit(new AuditEvent("Added", "User", "", "", userInfo.name, currentUser.getName()));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean delUser(BUser currentUser, UserInfoItem userInfo) {
        if (!currentUser.getPermissions().isSuperUser()) {
            return false;
        }
        BUserService userService = (BUserService) Sys.getService(BUserService.TYPE);
        try {
            userService.remove(userInfo.name);
            BAuditHistoryService a = (BAuditHistoryService) Sys.getService(BAuditHistoryService.TYPE);
            a.audit(new AuditEvent("Removed", "User", "", userInfo.name, "", currentUser.getName()));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static UserInfoItem getUser(BUser currentUser, UserInfoItem userInfo) {
        List<UserInfoItem> items = listUsers(currentUser);
        for (UserInfoItem item : items) {
            if (StringUtils.isNotBlank(item.name) && item.name.equals(userInfo.name)) {
                return item;
            }
        }
        return null;
    }

    public static boolean editUser(BUser currentUser, UserInfoItem userInfo) {
        if (!currentUser.getPermissions().isSuperUser()) {
            return false;
        }
        BUserService userService = (BUserService) Sys.getService(BUserService.TYPE);
        try {
            BUser user = userService.getUser(userInfo.name);
            if (StringUtils.isNotBlank(userInfo.password)) {
                BPasswordAuthenticator pa = (BPasswordAuthenticator) user.getAuthenticator();
                pa.setPassword(BPassword.make(userInfo.password));
            }
            user.setEmail(userInfo.email);
            user.setCellPhoneNumber(userInfo.phone);

            BAuditHistoryService a = (BAuditHistoryService) Sys.getService(BAuditHistoryService.TYPE);
            a.audit(new AuditEvent("Changed", "User", "", "", userInfo.name, currentUser.getName()));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
